using System;
using System.Linq;
using ilgolosone.DomainModel.Entities;
using ilgolosone.DomainModel.Repositories;
using ilgolosone.Persistence.NH.Data;
using ilgolosone.Persistence.NH.Data.Impl;
using NHibernate.Linq;

namespace ilgolosone.Persistence.NH.Repositories
{
    public class SpecialtyRepository : NHibernateRepository<Specialty>, ISpecialtyRepository
    {
        public SpecialtyRepository(INHibernateUnitOfWork unitOfWork) 
            : base(unitOfWork)
        {
        }

        public Specialty[] StoredProc()
        {
            return Session.CreateQuery("from Specialty").List<Specialty>().ToArray();
        }

        #region ISpecialtyRepository Members

        public Restaurant[] GetRestaurantBySpecialties(Guid idSpecialty)
        {
            Restaurant[] restaurants = (from d in Session.Linq<Restaurant>().Expand("Dishes")
                                        from r in d.Dishes
                                        where r.Specialty.Id == idSpecialty
                                        select r.Restaurant).ToArray();

            return restaurants;
        }

        public Specialty[] GetSpecialtiesByIngredients(string ingredients)
        {
            Specialty[] specs = (from s in Session.Linq<Specialty>().Expand("Dishes")
                                 from d in s.Dishes
                                 where d.Ingredients.Contains(ingredients)
                                 select s).ToArray();
            
            return specs;
        }

        #endregion        
    }
}